package com.salton123.app;

import android.app.Application;
import android.content.Context;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.Utils;
import com.hjq.toast.ToastUtils;
import com.hyphenate.easeim.BuildConfig;
import com.tencent.bugly.Bugly;

import androidx.multidex.MultiDex;

/**
 * User: newSalton@outlook.com
 * Date: 2019/5/9 17:50
 * ModifyTime: 17:50
 * Description:
 */
public class BaseApplication extends Application {
    public static Application sInstance;
    protected BaseAppDelegate mBaseAppDelegate;

    public static <T extends Application> T getInstance() {
        return (T) sInstance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        sInstance = this;
        mBaseAppDelegate = getAppDelegate();
    }

    public BaseAppDelegate getAppDelegate() {
        if (mBaseAppDelegate == null) {
            return new BaseAppDelegate(this) {
                @Override
                public void mediumPriority() {
                    super.mediumPriority();
                    Utils.init(sInstance);
                    Bugly.init(sInstance, BuildConfig.BUGLY_APP_ID, BuildConfig.APP_DEVELOP);

                }

                @Override
                public void highPriority() {
                    super.highPriority();
                    if (BuildConfig.APP_DEVELOP) {
                        // 这两行必须写在init之前，否则这些配置在init过程中将无效
                        ARouter.openLog();     // 打印日志
                        ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
                    }
                    ARouter.init(sInstance); // 尽可能早，推荐在Application中初始化
                }
            };
        } else {
            return mBaseAppDelegate;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (mBaseAppDelegate != null) {
            mBaseAppDelegate.onCreate();
        }
        ToastUtils.init(sInstance);
    }

}
